{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bike-Sharing-Dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入必要的工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from sklearn.metrics import r2_score\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 数据探索"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>dteday</th>\n",
       "      <th>season</th>\n",
       "      <th>yr</th>\n",
       "      <th>mnth</th>\n",
       "      <th>holiday</th>\n",
       "      <th>weekday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>weathersit</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>casual</th>\n",
       "      <th>registered</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2011-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.344167</td>\n",
       "      <td>0.363625</td>\n",
       "      <td>0.805833</td>\n",
       "      <td>0.160446</td>\n",
       "      <td>331</td>\n",
       "      <td>654</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2011-01-02</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.363478</td>\n",
       "      <td>0.353739</td>\n",
       "      <td>0.696087</td>\n",
       "      <td>0.248539</td>\n",
       "      <td>131</td>\n",
       "      <td>670</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2011-01-03</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.196364</td>\n",
       "      <td>0.189405</td>\n",
       "      <td>0.437273</td>\n",
       "      <td>0.248309</td>\n",
       "      <td>120</td>\n",
       "      <td>1229</td>\n",
       "      <td>1349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2011-01-04</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.212122</td>\n",
       "      <td>0.590435</td>\n",
       "      <td>0.160296</td>\n",
       "      <td>108</td>\n",
       "      <td>1454</td>\n",
       "      <td>1562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2011-01-05</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.226957</td>\n",
       "      <td>0.229270</td>\n",
       "      <td>0.436957</td>\n",
       "      <td>0.186900</td>\n",
       "      <td>82</td>\n",
       "      <td>1518</td>\n",
       "      <td>1600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant     dteday  season  yr  mnth  holiday  weekday  workingday  \\\n",
       "0        1 2011-01-01       1   0     1        0        6           0   \n",
       "1        2 2011-01-02       1   0     1        0        0           0   \n",
       "2        3 2011-01-03       1   0     1        0        1           1   \n",
       "3        4 2011-01-04       1   0     1        0        2           1   \n",
       "4        5 2011-01-05       1   0     1        0        3           1   \n",
       "\n",
       "   weathersit      temp     atemp       hum  windspeed  casual  registered  \\\n",
       "0           2  0.344167  0.363625  0.805833   0.160446     331         654   \n",
       "1           2  0.363478  0.353739  0.696087   0.248539     131         670   \n",
       "2           1  0.196364  0.189405  0.437273   0.248309     120        1229   \n",
       "3           1  0.200000  0.212122  0.590435   0.160296     108        1454   \n",
       "4           1  0.226957  0.229270  0.436957   0.186900      82        1518   \n",
       "\n",
       "    cnt  \n",
       "0   985  \n",
       "1   801  \n",
       "2  1349  \n",
       "3  1562  \n",
       "4  1600  "
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(\"/Users/admin/Desktop/Bike-Sharing-Dataset/day.csv\", parse_dates=['dteday'])\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  2.2 数据基本信息\n",
    "样本数目、特征维数\n",
    "每个特征的类型、空值样本的数目、数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(731, 16)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3 数据探索\n",
    "\n",
    "### 这里我们不考虑casual、registered注册用户对租车的影响\n",
    "\n",
    "根据前面数据的探索，根据数据的特点选择合适的模型类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['season', 'yr', 'mnth', 'holiday', 'weekday', 'weathersit', 'temp',\n",
       "       'hum', 'windspeed', 'cnt'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 移除无用的特征\n",
    "remove_columns = ['instant', 'atemp', 'dteday', 'workingday', 'casual', 'registered']\n",
    "data = data.drop(remove_columns, axis=1)\n",
    "data.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 one-hot编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "### one-hot编码1\n",
    "# col = ['season', 'weathersit', 'mnth', 'weekday']\n",
    "# for each in col:\n",
    "#     dummies = pd.get_dummies(data[each], prefix=each, drop_first=False)\n",
    "#     data = pd.concat([data, dummies], axis=1)\n",
    "# data = data.drop(col, axis=1)\n",
    "# data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>yr</th>\n",
       "      <th>temp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>cnt</th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>...</th>\n",
       "      <th>weekday_0</th>\n",
       "      <th>weekday_1</th>\n",
       "      <th>weekday_2</th>\n",
       "      <th>weekday_3</th>\n",
       "      <th>weekday_4</th>\n",
       "      <th>weekday_5</th>\n",
       "      <th>weekday_6</th>\n",
       "      <th>weathersit_1</th>\n",
       "      <th>weathersit_2</th>\n",
       "      <th>weathersit_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0.344167</td>\n",
       "      <td>0.805833</td>\n",
       "      <td>0.160446</td>\n",
       "      <td>985</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0.363478</td>\n",
       "      <td>0.696087</td>\n",
       "      <td>0.248539</td>\n",
       "      <td>801</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0.196364</td>\n",
       "      <td>0.437273</td>\n",
       "      <td>0.248309</td>\n",
       "      <td>1349</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.590435</td>\n",
       "      <td>0.160296</td>\n",
       "      <td>1562</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0.226957</td>\n",
       "      <td>0.436957</td>\n",
       "      <td>0.186900</td>\n",
       "      <td>1600</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   yr      temp       hum  windspeed   cnt  season_1  season_2  season_3  \\\n",
       "0   0  0.344167  0.805833   0.160446   985         1         0         0   \n",
       "1   0  0.363478  0.696087   0.248539   801         1         0         0   \n",
       "2   0  0.196364  0.437273   0.248309  1349         1         0         0   \n",
       "3   0  0.200000  0.590435   0.160296  1562         1         0         0   \n",
       "4   0  0.226957  0.436957   0.186900  1600         1         0         0   \n",
       "\n",
       "   season_4  mnth_1      ...       weekday_0  weekday_1  weekday_2  weekday_3  \\\n",
       "0         0       1      ...               0          0          0          0   \n",
       "1         0       1      ...               1          0          0          0   \n",
       "2         0       1      ...               0          1          0          0   \n",
       "3         0       1      ...               0          0          1          0   \n",
       "4         0       1      ...               0          0          0          1   \n",
       "\n",
       "   weekday_4  weekday_5  weekday_6  weathersit_1  weathersit_2  weathersit_3  \n",
       "0          0          0          1             0             1             0  \n",
       "1          0          0          0             0             1             0  \n",
       "2          0          0          0             1             0             0  \n",
       "3          0          0          0             1             0             0  \n",
       "4          0          0          0             1             0             0  \n",
       "\n",
       "[5 rows x 33 columns]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### one-hot编码2\n",
    "columns = ['season', 'mnth', 'holiday', 'weekday', 'weathersit']\n",
    "data = pd.get_dummies(data, columns=columns)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['yr', 'temp', 'hum', 'windspeed', 'cnt', 'season_1', 'season_2',\n",
       "       'season_3', 'season_4', 'mnth_1', 'mnth_2', 'mnth_3', 'mnth_4',\n",
       "       'mnth_5', 'mnth_6', 'mnth_7', 'mnth_8', 'mnth_9', 'mnth_10', 'mnth_11',\n",
       "       'mnth_12', 'holiday_0', 'holiday_1', 'weekday_0', 'weekday_1',\n",
       "       'weekday_2', 'weekday_3', 'weekday_4', 'weekday_5', 'weekday_6',\n",
       "       'weathersit_1', 'weathersit_2', 'weathersit_3'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.2标准化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>yr</th>\n",
       "      <th>temp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>cnt</th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>...</th>\n",
       "      <th>weekday_0</th>\n",
       "      <th>weekday_1</th>\n",
       "      <th>weekday_2</th>\n",
       "      <th>weekday_3</th>\n",
       "      <th>weekday_4</th>\n",
       "      <th>weekday_5</th>\n",
       "      <th>weekday_6</th>\n",
       "      <th>weathersit_1</th>\n",
       "      <th>weathersit_2</th>\n",
       "      <th>weathersit_3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>-0.826097</td>\n",
       "      <td>1.249316</td>\n",
       "      <td>-0.387626</td>\n",
       "      <td>-1.816709</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>-0.720601</td>\n",
       "      <td>0.478785</td>\n",
       "      <td>0.749089</td>\n",
       "      <td>-1.911691</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>-1.633538</td>\n",
       "      <td>-1.338358</td>\n",
       "      <td>0.746121</td>\n",
       "      <td>-1.628810</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>-1.613675</td>\n",
       "      <td>-0.263001</td>\n",
       "      <td>-0.389562</td>\n",
       "      <td>-1.518858</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>-1.466410</td>\n",
       "      <td>-1.340576</td>\n",
       "      <td>-0.046275</td>\n",
       "      <td>-1.499242</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   yr      temp       hum  windspeed       cnt  season_1  season_2  season_3  \\\n",
       "0   0 -0.826097  1.249316  -0.387626 -1.816709         1         0         0   \n",
       "1   0 -0.720601  0.478785   0.749089 -1.911691         1         0         0   \n",
       "2   0 -1.633538 -1.338358   0.746121 -1.628810         1         0         0   \n",
       "3   0 -1.613675 -0.263001  -0.389562 -1.518858         1         0         0   \n",
       "4   0 -1.466410 -1.340576  -0.046275 -1.499242         1         0         0   \n",
       "\n",
       "   season_4  mnth_1      ...       weekday_0  weekday_1  weekday_2  weekday_3  \\\n",
       "0         0       1      ...               0          0          0          0   \n",
       "1         0       1      ...               1          0          0          0   \n",
       "2         0       1      ...               0          1          0          0   \n",
       "3         0       1      ...               0          0          1          0   \n",
       "4         0       1      ...               0          0          0          1   \n",
       "\n",
       "   weekday_4  weekday_5  weekday_6  weathersit_1  weathersit_2  weathersit_3  \n",
       "0          0          0          1             0             1             0  \n",
       "1          0          0          0             0             1             0  \n",
       "2          0          0          0             1             0             0  \n",
       "3          0          0          0             1             0             0  \n",
       "4          0          0          0             1             0             0  \n",
       "\n",
       "[5 rows x 33 columns]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feats = [\"temp\", \"hum\", \"windspeed\", \"cnt\"]\n",
    "scaled_features = {}\n",
    "for each in feats:\n",
    "    mean, std = data[each].mean(), data[each].std()\n",
    "    scaled_features[each] = [mean, std]\n",
    "    data.loc[:, each] = (data[each] - mean)/std\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/admin/Documents/Anaconda5.2/anaconda3/lib/python3.6/site-packages/sklearn/model_selection/_split.py:2026: FutureWarning: From version 0.21, test_size will always complement train_size unless both are specified.\n",
      "  FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "train_data0, test_data0 = train_test_split(data[data['yr'] == 0], train_size=0.75)\n",
    "train_data1, test_data1 = train_test_split(data[data['yr'] == 1], train_size=0.75)\n",
    "y_train = np.hstack([train_data0['cnt'].values, train_data1['cnt'].values])\n",
    "y_test = np.hstack([test_data0['cnt'].values, test_data1['cnt'].values])\n",
    "X_train = np.vstack([train_data0.drop('cnt', axis=1).values, train_data1.drop('cnt', axis=1).values])\n",
    "X_test = np.vstack([test_data0.drop('cnt', axis=1).values, test_data1.drop('cnt', axis=1).values])\n",
    "\n",
    "\n",
    "# train_data, test_data = train_test_split(data, train_size=0.75)\n",
    "# y_train = train_data['cnt']\n",
    "# X_train = train_data.drop('cnt', axis=1)\n",
    "# y_test = test_data['cnt']\n",
    "# X_test = test_data.drop('cnt', axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.1 尝试缺省参数的线性回归"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7955181378337965\n",
      "0.8608930468591963\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.metrics import r2_score\n",
    "\n",
    "lr = LinearRegression()\n",
    "lr.fit(X_train, y_train)\n",
    "\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "y_train_pred_lr = lr.predict(X_train)\n",
    "\n",
    "print(r2_score(y_test, y_test_pred_lr))\n",
    "print(r2_score(y_train, y_train_pred_lr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>weathersit_1</td>\n",
       "      <td>3.601947e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>weathersit_2</td>\n",
       "      <td>3.601947e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>weathersit_3</td>\n",
       "      <td>3.601947e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>weekday_6</td>\n",
       "      <td>3.289189e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>weekday_5</td>\n",
       "      <td>3.289189e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>weekday_3</td>\n",
       "      <td>3.289189e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>weekday_4</td>\n",
       "      <td>3.289189e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>weekday_2</td>\n",
       "      <td>3.289189e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>weekday_1</td>\n",
       "      <td>3.289189e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>weekday_0</td>\n",
       "      <td>3.289189e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>season_4</td>\n",
       "      <td>3.197294e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>season_2</td>\n",
       "      <td>3.197294e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>season_3</td>\n",
       "      <td>3.197294e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>season_1</td>\n",
       "      <td>3.197294e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>yr</td>\n",
       "      <td>5.139645e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>temp</td>\n",
       "      <td>4.049890e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>hum</td>\n",
       "      <td>-1.125123e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>-1.309218e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>holiday_0</td>\n",
       "      <td>-1.715908e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>holiday_1</td>\n",
       "      <td>-1.715908e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>mnth_9</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>mnth_6</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>mnth_5</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>mnth_3</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>mnth_8</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>mnth_10</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>mnth_4</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>mnth_7</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>mnth_2</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>mnth_1</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>mnth_12</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>mnth_11</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         columns          coef\n",
       "29  weathersit_1  3.601947e+11\n",
       "30  weathersit_2  3.601947e+11\n",
       "31  weathersit_3  3.601947e+11\n",
       "28     weekday_6  3.289189e+11\n",
       "27     weekday_5  3.289189e+11\n",
       "25     weekday_3  3.289189e+11\n",
       "26     weekday_4  3.289189e+11\n",
       "24     weekday_2  3.289189e+11\n",
       "23     weekday_1  3.289189e+11\n",
       "22     weekday_0  3.289189e+11\n",
       "7       season_4  3.197294e+11\n",
       "5       season_2  3.197294e+11\n",
       "6       season_3  3.197294e+11\n",
       "4       season_1  3.197294e+11\n",
       "0             yr  5.139645e-01\n",
       "1           temp  4.049890e-01\n",
       "2            hum -1.125123e-01\n",
       "3      windspeed -1.309218e-01\n",
       "20     holiday_0 -1.715908e+11\n",
       "21     holiday_1 -1.715908e+11\n",
       "16        mnth_9 -2.638325e+11\n",
       "13        mnth_6 -2.638325e+11\n",
       "12        mnth_5 -2.638325e+11\n",
       "10        mnth_3 -2.638325e+11\n",
       "15        mnth_8 -2.638325e+11\n",
       "17       mnth_10 -2.638325e+11\n",
       "11        mnth_4 -2.638325e+11\n",
       "14        mnth_7 -2.638325e+11\n",
       "9         mnth_2 -2.638325e+11\n",
       "8         mnth_1 -2.638325e+11\n",
       "19       mnth_12 -2.638325e+11\n",
       "18       mnth_11 -2.638325e+11"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "colu = X_train.columns\n",
    "fs = pd.DataFrame({\"columns\": list(colu), \"coef\": list((lr.coef_.T))})\n",
    "fs.sort_values(by=['coef'], ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAekAAAFsCAYAAADos0H8AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAHWtJREFUeJzt3XuYXHWd5/H3l6QlDoHEQIKBqAkKKrck2CAZBIPxghMkzI6KLpewBFC8PI6uF9B1bBkd9YHVlZVxzaASBCWQHSaMrjOwkUyEVTDRyC1oIoIGYtJEuQRBSPjuH3U6dtLd6UqnqvvXqffrefqpOpc651u/qu5P/37n1KnITCRJUnn2GOoCJElS7wxpSZIKZUhLklQoQ1qSpEIZ0pIkFcqQliSpUIa0Wl5E3BMRM4e6jqEUEX8dEb+NiE0RMX0Q97spIg7qY9nZEXFrg/bzQES8vhHbkgaTIa3dWm9/nLf/45+Zh2Xm0n62MzkiMiJGNqnUoXYp8L7MHJ2ZP9t+YfXcn6xC9aGI+GJEjNjVnVb7u39XtyPtrgxpqQAFhP9LgHv6WWdqZo4GXgucBpzT9KqkFmdIq+V1721HxDERsTwiHo+I9RHxxWq1ZdXto1VvckZE7BER/y0iHoyIDRFxVUSM6bbds6plGyPik9vtpyMiFkXE1RHxOHB2te8fRcSjEbEuIr4SEc/rtr2MiPdExOqIeCIi/j4iXlo95vGIuK77+ts9x15rjYg9I2ITMAL4eUT8qr/2ysw1wG3AtG7bHxMRX6/qfigiPtPV046Il0XEf0TEYxHxSEQs3O45vay6v29E3Fg9lzuAl3Zbr8dIRkQsjYhzq/svjYgfVG39SERcExFj+2iLvl5jqTiGtLStLw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V+E7eI2l4hjS0rZOAu6pznj+MvCO6jjqH4HPArdVQ7rHAt8AvkXtzO9fUzvp6f0A1THj9wPXUutVPwFsoNbj7MuHgf9crftPwMIdrLuz+qx1IDLzLuA/+HMYngU8D7iXWhAvonYCGMDRwO1Vm94IfCAzf93LZt9HbTj9d8CVwDe3W35etb+NwGHA/+u27NPUhuMfA75HLfD70utrvONnLA2NyOxtFE9SI1W910epDWX3FlCS1IM9aalJIuIt1dDtXtSu6HUX8MDQViVpODGkpeaZQ+2ErYeBg6kNqzp0JaluDndLklQoe9KSJBVqUC/qv99+++XkyZMHc5eSJBVlxYoVj2Tm+HrWHdSQnjx5MsuXLx/MXUqSVJSIqPtqeg53S5JUKENakqRCGdKSJBVqUI9JS5J69+yzz7J27VqeftorlO4uRo0axaRJk2hraxvwNgxpSSrA2rVr2XvvvZk8eTIRjfryMw2VzGTjxo2sXbuWKVOmDHg7DndLUgGefvpp9t13XwN6NxER7Lvvvrs8MmJIS1IhDOjdSyNeT0NakqRCeUxakgrU0TH42xsxYgRHHHEEmzdvZsqUKXzrW99i7NixO72vc889lw996EMceuih28y/8sorWb58OV/5yld2epsAo0ePZtOmTXWtO3PmTC699FLa29u3zlu+fDlXXXUVl1122YD2PxTsSUuSAHj+85/PypUrufvuuxk3bhyXX375gLZzxRVX9AjoErS3tzc9oLds2dLQ7RnSkqQeZsyYwUMPPbR1+pJLLuHoo4/myCOP5FOf+hQATz75JLNnz2bq1KkcfvjhLFy4EKj1YrsuAf3Nb36TQw45hNe+9rXcdtttW7d39tlns2jRoq3To0ePBmDTpk3MmjWLo446iiOOOILFixf3qG3dunWccMIJTJs2jcMPP5wf/vCHdT2npUuXcvLJJwPQ0dHBOeecw8yZMznooIO2Ce+rr76aY445hmnTpvGud71ra/BecMEFtLe3c9hhh21tA6hd8vriiy/mNa95Dddff31dtdTL4W5J0ja2bNnCkiVLmDdvHgA33XQTq1ev5o477iAzOeWUU1i2bBmdnZ0ccMABfO973wPgscce22Y769at41Of+hQrVqxgzJgxnHjiiUyfPn2H+x41ahQ33HAD++yzD4888gjHHnssp5xyyjYnYX3729/mTW96E5/4xCfYsmULf/zjHwf0PO+77z5uueUWnnjiCV7+8pdzwQUXsGbNGhYuXMhtt91GW1sb73nPe7jmmms466yz+OxnP8u4cePYsmULs2bN4s477+TII4/cWvett946oDp2xJCWJAHw1FNPMW3aNB544AFe9apX8YY3vAGohfRNN920NWA3bdrE6tWrOf744/nwhz/Mxz72MU4++WSOP/74bbZ3++23M3PmTMaPr33h02mnncYvf/nLHdaQmXz84x9n2bJl7LHHHjz00EOsX7+eF77whVvXOfrooznnnHN49tlnOfXUU5k2bdqAnu/s2bPZc8892XPPPZkwYQLr169nyZIlrFixgqOPPnprm0yYMAGA6667jvnz57N582bWrVvHvffeuzWkTzvttAHV0B+HuyVJwJ+PST/44IM888wzW49JZyYXXXQRK1euZOXKlaxZs4Z58+ZxyCGHsGLFCo444gguuugiLr744h7b7OtjSCNHjuS5557buv1nnnkGgGuuuYbOzk5WrFjBypUr2X///Xt81viEE05g2bJlHHjggZx55plcddVVA3q+e+6559b7I0aMYPPmzWQmc+fO3fpcf/GLX9DR0cGvf/1rLr30UpYsWcKdd97J7Nmzt6lrr732GlAN/bEnLUkDUO/Z140+S3swjBkzhssuu4w5c+ZwwQUX8KY3vYlPfvKTnH766YwePZqHHnqItrY2Nm/ezLhx4zjjjDMYPXo0V1555TbbefWrX80HPvABNm7cyD777MP111/P1KlTgdpx3BUrVvD2t7+dxYsX8+yzzwK1IfMJEybQ1tbGLbfcwoMP9vxWxwcffJADDzyQ8847jyeffJKf/vSnnHXWWQ157rNmzWLOnDl88IMfZMKECfz+97/niSee4PHHH2evvfZizJgxrF+/nu9///vMnDmzIfvcEUNakgo01OE+ffp0pk6dyrXXXsuZZ57JqlWrmDFjBlA7yevqq69mzZo1fOQjH2GPPfagra2Nr371q9tsY+LEiXR0dDBjxgwmTpzIUUcdtfUkrPPOO485c+ZwzDHHMGvWrK090dNPP523vOUttLe3M23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vNXjamwCPW87rvde6Mf9TzffwW+k5l/ioh3UxtpeF3TKytXU98jRYd0Zr5+R8sjYi5wMjAre//A92536dL+2qTObTxc3W6IiBuoDXEN25BuQJu01PskItZHxMTMXFcNy23oYxtd75P7I2IpMB3YnUK6nte9a521ETESGMOOhz6Hu37bJDM3dpv8J3bz4/R1aOrfj2E73B0RJwEfA07JzD/2sZqXLt1OROwVEXt33QfeSO3kqlbWau+TG4G51f25QI/Rhoh4QUTsWd3fDzgOuHfQKhwc9bzu3dvqrcAP+ugQ7C76bZPtjreeAqwaxPpKdCNwVnWW97HAY12HkxoiM4flD7CG2nGAldXP/6rmHwD8n27r/RW1s79/RW34c8hrb2Kb/DW1/+r+BKwH/n37NgEOAn5e/dxjm7Tk+2Rfamd1r65ux1Xz24Erqvt/CdxVvU/uAuYNdd1NaoserztwMbV//gFGAddXf2/uAA4a6poLaJPPVX87fg7cArxiqGtucnt8B1gHPFv9LZkHvBt4d7U8qJ0R/6vqd6W9kfv3sqCSJBVq2A53S5K0uzOkJUkqlCEtSVKhDGlJkgplSEuSVChDWpKkQhnSkiQV6v8DwDQPJe/M78kAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "f, ax = plt.subplots(figsize=(7, 5))\n",
    "f.tight_layout()\n",
    "ax.hist(y_train - y_train_pred_lr, bins=40, label='Residuals Linear', color='b', alpha=.5); \n",
    "ax.set_title(\"Histogram of Residuals\") \n",
    "ax.legend(loc='best');"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.2 正则化的线性回归（L2正则 --> 岭回归）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7955181378337965\n",
      "0.8608930468591963\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import RidgeCV\n",
    "\n",
    "alphas = [0.01, 0.1, 1, 10, 100]\n",
    "ridge = RidgeCV(alphas=alphas, store_cv_values=True)\n",
    "ridge.fit(X_train, y_train)\n",
    "\n",
    "y_test_pred_ridge = ridge.predict(X_test)\n",
    "y_train_pred_ridge = ridge.predict(X_train)\n",
    "\n",
    "print(r2_score(y_test, y_test_pred_lr))\n",
    "print(r2_score(y_train, y_train_pred_lr))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD8CAYAAAB3u9PLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAIABJREFUeJzt3XlwHvWd5/H3R5fl+5TBWOYKTsCAOSw75CIJEGJIYiAB7GySIbXsspktqnYrtakwlRpmh9nsVJKqzVZ22ZkwOySZqiSSTcLESUwMIeSYHY5HNr6NgzHEj2xjy/jAl6zru388LfMgZKllS+pH0udV9ZT66f5197dbdn+epw/9FBGYmZmVZV2AmZmVBgeCmZkBDgQzM0s4EMzMDHAgmJlZwoFgZmaAA8HMzBIOBDMzAxwIZmaWqMi6gP6YMWNGXHjhhVmXYWY2rKxZs2Z/RNT01W5YBcKFF15IY2Nj1mWYmQ0rkv6Upp1PGZmZGeBAMDOzhAPBzMwAB4KZmSUcCGZmBjgQzMws4UAwMzPAgWBmVtKaDh7nG796iX1HWgZ9XQ4EM7MStqKxib//3Su0tncO+rocCGZmJaqjM1jRmOdDc2uonTpu0NfnQDAzK1F/eLmZ3YdbWLZwzpCsz4FgZlaiGnJ5po2v4qbLzhmS9TkQzMxK0P6jJ3lqy14+c+1sqiqG5lDtQDAzK0E/XdtEe2ewdIhOF4EDwcys5EQE9bk8dRdM5ZKZE4dsvQ4EM7MS0/ing+xo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rGvL91Y/FnJ/sr4uB30jaGBGvnG1t3aTZ/kHZR31Is86fAz+OiJOSvkThW8wNg1xXGlnsr76spfCnHo5KuhX4Z2DuUK1c0gTgJ8B/jog3u0/uYZYB21/DOhAi4qbepku6B/gkcGMkJ+C6aQKKPynVArsHu66Uy9id/Nwn6XEKpwXOKhAGoK4h31+S9kqaFRF7kq/G+06zjK79tUPSbyl8uhroQEiz/V1tmiRVAJMZ/NMTfdYVEW8Uvf0HCtfVSsGg/Js6G8UH4YhYJen/SJoREYP+N44kVVIIgx9GxE97aDKo+2vEnjKStBj4KrAkIo6fplkOmCvpIklVFC4CDtodKmlJGi9pYtcwhQvkPd4RMcSy2F8rgXuS4XuAd3yTkTRV0phkeAbwAWDLINSSZvuL670T+M1pPowMaV3dzjMvoXB+uhSsBP4suXvmOuBw1ynCrEg6t+u6j6RFFI6Tb/Q+14CsV8A/Alsj4n+cptng7q+hvpI+VC9gO4VzbeuSV9edH+cBq4ra3Urhav4rFE6dDHZdd1BI+ZPAXmB197oo3C2yPnltLpW6Mtpf04GngZeTn9OS8XXA/02G3w9sTPbXRuDeQaznHdsPPEThgwdANbAi+ff3AnDxYO+jlHX9bfJvaT3wDHDpENX1Y2AP0Jb8+7oX+BLwpWS6gIeTujfSy513Q1jT/UX76jng/UO0rz5I4fTPhqLj1q1Dub/8pLKZmQEj+JSRmZn1jwPBzMwAB4KZmSUcCGZmBjgQzMws4UAwMzPAgWBmZgkHgpmZAfD/AZ4aS3H9hs9sAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0\n"
     ]
    }
   ],
   "source": [
    "mse_mean = np.mean(ridge.cv_values_, axis=0)\n",
    "plt.plot(np.log10(alphas), mse_mean.reshape(len(alphas), 1))\n",
    "plt.show()\n",
    "\n",
    "print(ridge.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>weathersit_1</td>\n",
       "      <td>1.087301e+12</td>\n",
       "      <td>0.441368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>weathersit_2</td>\n",
       "      <td>1.087301e+12</td>\n",
       "      <td>0.185547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>weathersit_3</td>\n",
       "      <td>1.087301e+12</td>\n",
       "      <td>-0.626915</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>season_4</td>\n",
       "      <td>3.696432e+11</td>\n",
       "      <td>0.439075</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>season_3</td>\n",
       "      <td>3.696432e+11</td>\n",
       "      <td>-0.008731</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>season_2</td>\n",
       "      <td>3.696432e+11</td>\n",
       "      <td>0.001136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>season_1</td>\n",
       "      <td>3.696432e+11</td>\n",
       "      <td>-0.431480</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>mnth_9</td>\n",
       "      <td>2.913595e+11</td>\n",
       "      <td>0.320128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>mnth_5</td>\n",
       "      <td>2.913595e+11</td>\n",
       "      <td>0.176334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>mnth_4</td>\n",
       "      <td>2.913595e+11</td>\n",
       "      <td>0.071279</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>mnth_6</td>\n",
       "      <td>2.913595e+11</td>\n",
       "      <td>0.051294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>mnth_3</td>\n",
       "      <td>2.913595e+11</td>\n",
       "      <td>0.069546</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>mnth_8</td>\n",
       "      <td>2.913595e+11</td>\n",
       "      <td>0.055547</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>mnth_10</td>\n",
       "      <td>2.913595e+11</td>\n",
       "      <td>0.070842</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>mnth_2</td>\n",
       "      <td>2.913595e+11</td>\n",
       "      <td>-0.102043</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>mnth_7</td>\n",
       "      <td>2.913595e+11</td>\n",
       "      <td>-0.126764</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>mnth_1</td>\n",
       "      <td>2.913595e+11</td>\n",
       "      <td>-0.116441</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>mnth_11</td>\n",
       "      <td>2.913595e+11</td>\n",
       "      <td>-0.223141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>mnth_12</td>\n",
       "      <td>2.913595e+11</td>\n",
       "      <td>-0.246581</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>weekday_4</td>\n",
       "      <td>9.553827e+10</td>\n",
       "      <td>0.058582</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>weekday_6</td>\n",
       "      <td>9.553827e+10</td>\n",
       "      <td>0.058567</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>weekday_3</td>\n",
       "      <td>9.553827e+10</td>\n",
       "      <td>0.051986</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>weekday_5</td>\n",
       "      <td>9.553827e+10</td>\n",
       "      <td>0.044225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>weekday_2</td>\n",
       "      <td>9.553827e+10</td>\n",
       "      <td>-0.017526</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>weekday_1</td>\n",
       "      <td>9.553827e+10</td>\n",
       "      <td>-0.077203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>weekday_0</td>\n",
       "      <td>9.553827e+10</td>\n",
       "      <td>-0.118630</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>yr</td>\n",
       "      <td>1.044252e+00</td>\n",
       "      <td>1.035894</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>temp</td>\n",
       "      <td>4.237447e-01</td>\n",
       "      <td>0.433601</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>-1.107890e-01</td>\n",
       "      <td>-0.112694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>hum</td>\n",
       "      <td>-1.230484e-01</td>\n",
       "      <td>-0.127223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>holiday_0</td>\n",
       "      <td>-5.696229e+11</td>\n",
       "      <td>0.097268</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>holiday_1</td>\n",
       "      <td>-5.696229e+11</td>\n",
       "      <td>-0.097268</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         columns       coef_lr  coef_ridge\n",
       "29  weathersit_1  1.087301e+12    0.441368\n",
       "30  weathersit_2  1.087301e+12    0.185547\n",
       "31  weathersit_3  1.087301e+12   -0.626915\n",
       "7       season_4  3.696432e+11    0.439075\n",
       "6       season_3  3.696432e+11   -0.008731\n",
       "5       season_2  3.696432e+11    0.001136\n",
       "4       season_1  3.696432e+11   -0.431480\n",
       "16        mnth_9  2.913595e+11    0.320128\n",
       "12        mnth_5  2.913595e+11    0.176334\n",
       "11        mnth_4  2.913595e+11    0.071279\n",
       "13        mnth_6  2.913595e+11    0.051294\n",
       "10        mnth_3  2.913595e+11    0.069546\n",
       "15        mnth_8  2.913595e+11    0.055547\n",
       "17       mnth_10  2.913595e+11    0.070842\n",
       "9         mnth_2  2.913595e+11   -0.102043\n",
       "14        mnth_7  2.913595e+11   -0.126764\n",
       "8         mnth_1  2.913595e+11   -0.116441\n",
       "18       mnth_11  2.913595e+11   -0.223141\n",
       "19       mnth_12  2.913595e+11   -0.246581\n",
       "26     weekday_4  9.553827e+10    0.058582\n",
       "28     weekday_6  9.553827e+10    0.058567\n",
       "25     weekday_3  9.553827e+10    0.051986\n",
       "27     weekday_5  9.553827e+10    0.044225\n",
       "24     weekday_2  9.553827e+10   -0.017526\n",
       "23     weekday_1  9.553827e+10   -0.077203\n",
       "22     weekday_0  9.553827e+10   -0.118630\n",
       "0             yr  1.044252e+00    1.035894\n",
       "1           temp  4.237447e-01    0.433601\n",
       "3      windspeed -1.107890e-01   -0.112694\n",
       "2            hum -1.230484e-01   -0.127223\n",
       "20     holiday_0 -5.696229e+11    0.097268\n",
       "21     holiday_1 -5.696229e+11   -0.097268"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fs = pd.DataFrame({\"columns\": list(colu), \"coef_lr\": list((lr.coef_.T)), \"coef_ridge\": list((ridge.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'], ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.3 正则化的线性回归（L1正则 --> Lasso）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LassoCV on test is 0.8534651597823907\n",
      "The r2 score of LassoCV on train is 0.8412385211679717\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LassoCV\n",
    "lasso = LassoCV()  \n",
    "lasso.fit(X_train, y_train)  \n",
    "y_test_pred_lasso = lasso.predict(X_test)\n",
    "y_train_pred_lasso = lasso.predict(X_train)\n",
    "\n",
    "print('The r2 score of LassoCV on test is', r2_score(y_test, y_test_pred_lasso))\n",
    "print('The r2 score of LassoCV on train is', r2_score(y_train, y_train_pred_lasso))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD8CAYAAACMwORRAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAHalJREFUeJzt3Xl0HOWd7vHvT2otlmVtluRNtuVFNjbYxkbYBAN2hs2QXCBkg4QJWT03CUnmTMIJuclh7pCFJJPMzXKzES5nyGQCQxISHDBjCMEBGwgWe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/dwRBfvHoB69fM1YW/xpDCXUTGVGVjO195dDdbyutZNSePb793KXPyJ3pdlu8p3EVkTLR19fLzvx7mvucqCCQZ37jpAj60cpbONB0nCncRiareYIg/bD/O9546QM2pLm68cDpfXnce03MmeF1aQlG4i0hU9If6/322nKMN7SwryuanH17BRbPzvC4tISncReSchEKOP+2q5t+ePsjRhnbOn57FLz9SylWLCnUykocU7iJy1p7dX8t3/ns/+99oYdE0hXosUbiLyKg1d/TwLxv28Oj24xRPzuBHty7n3Uum6cvSGKJwF5FReaG8ni/9dic1LV18/soSPvd380lJ1u2YY43CXUQi9si2Su56dBfF+RN59NOXsmxmjtclyTAU7iISkfufr+AbT+zjigUF/Py2FbpJdYzT1hGRM3LO8YM/H+KHzxzi+iVT+cEHl5Ma0DBMrFO4i8iwnHN884l93L/ldT5QWsS9Ny8lWV+axgWFu4gMKRRyfO2x1/jN347x0UuLdZPqOKNwF5G36QmG+PLvdvHo9uN8Zu087rx2oY5djzMKdxF5i6b2bj7zn6/ywuEGvnj1Aj53ZYnXJclZULiLyGnlta188sFtVDd18r33L+N9FxV5XZKcJYW7iADw36+d4M7f7SItkMRD61fpgl9xTuEukuDau3v5+uN7eejlSpaGr+RYlJvhdVlyjhTuIglsR2UTX3xkB4fr2viHNXP54tULdQy7TyjcRRJQS2cP33/qIA++eITCSWn8+hOruKwk3+uyJIoU7iIJpP/a6/du3E9NSycfuWQ2X7p2IZPSU7wuTaJM4S6SIF4or+dbT+7jteOnOH96Fj+7bQXLZ+V6XZaMEYW7iI8559hSXs9Pnz3MixUNzMiZwA8+eCE3LJuus019TuEu4kPdvSE27XmDXz5fwa6qZqZkpfG1dy3itktmk56S7HV5Mg4U7iI+UtnYziNllTz0ciX1rV0UT87g3puXcPOKGaQFFOqJROEuEuea23t4fHc1f9x+nG1HTmIGV55XyG2XzOaKkgINvyQohbtIHAqG+sbSf1tWyVN7augOhphfmMmd1y7kpuUzmJEzwesSxWMKd5E44Zxj9/FmHttRzeO7qqk51UVORg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5KXD/gHYfB8rDj495XfcwfSmnb7xzR/jx8/Dy6cDG8PRc+o5g2Ansoe/Pbc9rP5u+hOevBw7StzcVc30B3kPf3mwXUANsiuNtMmJf4mGbhGucDDwDHAo/54WXn/69By4Fdoe3y27gE17Xfa4PnaEqIuJDCTUsIyKSKBTuIiI+pHAXEfEhhbuIiA8p3EVEfEjhLiLiQwp3EREfUriLiPjQ/weWV0Q7mwVOKQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.001242832315011635\n"
     ]
    }
   ],
   "source": [
    "mses = np.mean(lasso.mse_path_, axis=1)\n",
    "plt.plot(np.log10(lasso.alphas_), mses)\n",
    "plt.show()\n",
    "\n",
    "print(lasso.alpha_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>coef_lasso</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>weathersit_1</td>\n",
       "      <td>3.601947e+11</td>\n",
       "      <td>2.252208e-01</td>\n",
       "      <td>2.252208e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>weathersit_2</td>\n",
       "      <td>3.601947e+11</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>weathersit_3</td>\n",
       "      <td>3.601947e+11</td>\n",
       "      <td>-7.348897e-01</td>\n",
       "      <td>-7.348897e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>weekday_6</td>\n",
       "      <td>3.289189e+11</td>\n",
       "      <td>5.556052e-02</td>\n",
       "      <td>5.556052e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>weekday_5</td>\n",
       "      <td>3.289189e+11</td>\n",
       "      <td>1.149318e-02</td>\n",
       "      <td>1.149318e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>weekday_3</td>\n",
       "      <td>3.289189e+11</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>weekday_4</td>\n",
       "      <td>3.289189e+11</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>weekday_2</td>\n",
       "      <td>3.289189e+11</td>\n",
       "      <td>-4.622865e-02</td>\n",
       "      <td>-4.622865e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>weekday_1</td>\n",
       "      <td>3.289189e+11</td>\n",
       "      <td>-1.197622e-01</td>\n",
       "      <td>-1.197622e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>weekday_0</td>\n",
       "      <td>3.289189e+11</td>\n",
       "      <td>-2.329463e-01</td>\n",
       "      <td>-2.329463e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>season_4</td>\n",
       "      <td>3.197294e+11</td>\n",
       "      <td>2.541213e-01</td>\n",
       "      <td>2.541213e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>season_2</td>\n",
       "      <td>3.197294e+11</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>season_3</td>\n",
       "      <td>3.197294e+11</td>\n",
       "      <td>-7.233241e-02</td>\n",
       "      <td>-7.233241e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>season_1</td>\n",
       "      <td>3.197294e+11</td>\n",
       "      <td>-5.428920e-01</td>\n",
       "      <td>-5.428920e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>yr</td>\n",
       "      <td>5.139645e-01</td>\n",
       "      <td>5.101666e-01</td>\n",
       "      <td>5.101666e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>temp</td>\n",
       "      <td>4.049890e-01</td>\n",
       "      <td>4.389739e-01</td>\n",
       "      <td>4.389739e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>hum</td>\n",
       "      <td>-1.125123e-01</td>\n",
       "      <td>-1.168105e-01</td>\n",
       "      <td>-1.168105e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>-1.309218e-01</td>\n",
       "      <td>-1.332898e-01</td>\n",
       "      <td>-1.332898e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>holiday_0</td>\n",
       "      <td>-1.715908e+11</td>\n",
       "      <td>1.182760e-01</td>\n",
       "      <td>1.182760e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>holiday_1</td>\n",
       "      <td>-1.715908e+11</td>\n",
       "      <td>-4.023940e-16</td>\n",
       "      <td>-4.023940e-16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>mnth_9</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "      <td>2.317232e-01</td>\n",
       "      <td>2.317232e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>mnth_6</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "      <td>4.858919e-02</td>\n",
       "      <td>4.858919e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>mnth_5</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "      <td>6.700096e-02</td>\n",
       "      <td>6.700096e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>mnth_3</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "      <td>1.147929e-01</td>\n",
       "      <td>1.147929e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>mnth_8</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "      <td>0.000000e+00</td>\n",
       "      <td>0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>mnth_10</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "      <td>5.308185e-02</td>\n",
       "      <td>5.308185e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>mnth_4</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "      <td>-0.000000e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>mnth_7</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "      <td>-2.125611e-01</td>\n",
       "      <td>-2.125611e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>mnth_2</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "      <td>-5.993659e-02</td>\n",
       "      <td>-5.993659e-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>mnth_1</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "      <td>-1.198042e-01</td>\n",
       "      <td>-1.198042e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>mnth_12</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "      <td>-1.576152e-01</td>\n",
       "      <td>-1.576152e-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>mnth_11</td>\n",
       "      <td>-2.638325e+11</td>\n",
       "      <td>-1.963649e-01</td>\n",
       "      <td>-1.963649e-01</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         columns       coef_lr    coef_ridge    coef_lasso\n",
       "29  weathersit_1  3.601947e+11  2.252208e-01  2.252208e-01\n",
       "30  weathersit_2  3.601947e+11 -0.000000e+00 -0.000000e+00\n",
       "31  weathersit_3  3.601947e+11 -7.348897e-01 -7.348897e-01\n",
       "28     weekday_6  3.289189e+11  5.556052e-02  5.556052e-02\n",
       "27     weekday_5  3.289189e+11  1.149318e-02  1.149318e-02\n",
       "25     weekday_3  3.289189e+11  0.000000e+00  0.000000e+00\n",
       "26     weekday_4  3.289189e+11  0.000000e+00  0.000000e+00\n",
       "24     weekday_2  3.289189e+11 -4.622865e-02 -4.622865e-02\n",
       "23     weekday_1  3.289189e+11 -1.197622e-01 -1.197622e-01\n",
       "22     weekday_0  3.289189e+11 -2.329463e-01 -2.329463e-01\n",
       "7       season_4  3.197294e+11  2.541213e-01  2.541213e-01\n",
       "5       season_2  3.197294e+11  0.000000e+00  0.000000e+00\n",
       "6       season_3  3.197294e+11 -7.233241e-02 -7.233241e-02\n",
       "4       season_1  3.197294e+11 -5.428920e-01 -5.428920e-01\n",
       "0             yr  5.139645e-01  5.101666e-01  5.101666e-01\n",
       "1           temp  4.049890e-01  4.389739e-01  4.389739e-01\n",
       "2            hum -1.125123e-01 -1.168105e-01 -1.168105e-01\n",
       "3      windspeed -1.309218e-01 -1.332898e-01 -1.332898e-01\n",
       "20     holiday_0 -1.715908e+11  1.182760e-01  1.182760e-01\n",
       "21     holiday_1 -1.715908e+11 -4.023940e-16 -4.023940e-16\n",
       "16        mnth_9 -2.638325e+11  2.317232e-01  2.317232e-01\n",
       "13        mnth_6 -2.638325e+11  4.858919e-02  4.858919e-02\n",
       "12        mnth_5 -2.638325e+11  6.700096e-02  6.700096e-02\n",
       "10        mnth_3 -2.638325e+11  1.147929e-01  1.147929e-01\n",
       "15        mnth_8 -2.638325e+11  0.000000e+00  0.000000e+00\n",
       "17       mnth_10 -2.638325e+11  5.308185e-02  5.308185e-02\n",
       "11        mnth_4 -2.638325e+11 -0.000000e+00 -0.000000e+00\n",
       "14        mnth_7 -2.638325e+11 -2.125611e-01 -2.125611e-01\n",
       "9         mnth_2 -2.638325e+11 -5.993659e-02 -5.993659e-02\n",
       "8         mnth_1 -2.638325e+11 -1.198042e-01 -1.198042e-01\n",
       "19       mnth_12 -2.638325e+11 -1.576152e-01 -1.576152e-01\n",
       "18       mnth_11 -2.638325e+11 -1.963649e-01 -1.963649e-01"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fs = pd.DataFrame({\"columns\": list(colu), \"coef_lr\": list((lr.coef_.T)), \"coef_ridge\": list((lasso.coef_.T)), \"coef_lasso\": list((lasso.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'], ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 总结"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从上图可以看出，weathersit_3、yr、temp、season_1影响自行车租赁的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
